package nbcp.base.model

/**
 * 主备两个存储对象。可以想像是两个逻辑蓄水池: 进水池, 流出池
 * 进水时:
 *   masterOpen ===> masterPool
 *              else alternatePool
 *
 * 流出时:
 *   masterOpen ===> alternatePool
 *              else masterPool
 *
 */

abstract class DualPoolData<T>(protected val masterPool: T, protected val alternatePool: T) {

    protected var masterOpen = false;
    protected val inputPool: T
        get() {
            if (masterOpen) return masterPool;
            else return alternatePool;
        }


    protected fun shift() {
        masterOpen = !masterOpen;
    }

    protected val outputPool: T
        get() {
            if (masterOpen) return alternatePool;
            else return masterPool;
        }

    protected abstract fun consumePool(pool: T);

    /**
     * 缓存的数据总量
     */
    abstract val total: Int

    fun consumeTask() {
        consumePool(outputPool);
        this.shift()
        consumePool(outputPool);
        this.shift()
    }
}